<?php

namespace App\Admin\Controllers;

use App\Department;
use App\Type;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class HandbookTypeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '文档类型';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Type);
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableView(); // 去掉查看
        });
        $grid->model()->orderBy('id','desc');
        $de_list = Department::where('id','>',0)->pluck('department_name', 'id')->toArray();
        $grid->column('id', __('Id'));
        $grid->column('type_name', '类型名称');
        $grid->column('depart_id', __('所属部门'))->select($de_list)->width(120);
        $grid->column('sort', '排序')->editable();
        $grid->column('created_at', '创建时间');
        $grid->column('updated_at', '更新时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Type::findOrFail($id));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Type);
        $de_list = Department::where('id','>',0)->pluck('department_name', 'id')->toArray();

        $form->text('type_name', '类型名称');
        $form->select('depart_id', '所属部门')->options($de_list);
        $form->number('sort', '排序')->default(1);

        return $form;
    }
}
